const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const session = require('express-session')

const app = express()
app.use('/static',express.static(path.join(__dirname,'public')))
app.use(cookieParser())
app.use(session({
    secret:'123456',
    cookie:{
        maxAge:1000*60*2
    }
}))

app.get('/count',(req,res)=>{
    if(!req.session.count){
        req.session.count=1
    }
    req.session.count++
    res.send('count number :'+req.session.count)
})

app.get('/checkstatus',(req,res)=>{
    console.log(req.session);
    res.send(`count count :${req.session.count}`)
})


app.get('/',(req,res)=>{
    let cookies = req.cookies
    res.send(cookies)
})

app.get('/add/cookie',(req,res)=>{
    let {uname,uage} = req.query
    // res.cookie('uname',uname)
    // res.cookie('uage',uage)
    res.cookie('uname',uname,{domain:'xxx.xxx.com',path:'/adimin',secure:true})
    res.cookie('uage',uage,{expires:new Date(Date.now()+90000),httpOnly:true})
    res.cookie('uage1',1,{maxAge:1000*3,httpOnly:true})
    res.cookie('uage2',2,{maxAge:1000*3,httpOnly:true})
    res.cookie('uage3',3,{maxAge:1000*3,httpOnly:true})
    res.send('设置成功')
})

app.get('/delete/cookie',(req,res)=>{
    res.clearCookie('uage')
    res.send('删除成功')
})



app.listen(3000,()=>{
    console.log(`服务已启动...`);
})